package com.wanmait.movies.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.Actor;
import com.wanmait.movies.vo.Director;

public class MovieDirectorDao {
	private MovieDirectorDao() {};
	private static MovieDirectorDao a=new MovieDirectorDao();
	public static MovieDirectorDao getMovieDirectorDao() {
		return a;
	}
	public void deleteDirectorMovie(int id) {
		Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql ="delete from movie_director  where director_id=?";
		try {
			
			ps=co.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
		DBHelper.getDBHelper().close(rs, ps, co);
		}
	}
	//删除
	
		public void deleteDirector(int id) {
			Connection co = DBHelper.getDBHelper().getconn();
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			String sql ="delete from movie_director  where movie_id=?";
			try {
				
				ps=co.prepareStatement(sql);
				ps.setInt(1, id);
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
			DBHelper.getDBHelper().close(rs, ps, co);
			}
		}
	
	
	//���ݵ�ӰID��ȡ��������
	public ArrayList<Director> movieDirectorGetMovieId(int movieId) {
		ArrayList<Director> allDirector=new ArrayList<>();
		DBHelper a=DBHelper.getDBHelper();
		Connection co = a.getconn();
		String sql="select * from movie_director,director where director_id=director.id and movie_id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = co.prepareStatement(sql);
			ps.setInt(1, movieId);
			rs=ps.executeQuery();
			while(rs.next()){
				Director cc=new Director();
				cc.setDirectorName(rs.getString("director_name"));
				cc.setDirectorImage(rs.getString("director_image"));
				cc.setId(rs.getInt("director.id"));
				allDirector.add(cc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			a.close(rs, ps, co);
			return allDirector;
		}
	}
	
	//添加一部电影所对应的所有导演
	public void addMovieDirector(String ids,int movieId) {
		DBHelper a=DBHelper.getDBHelper();
		Connection co = a.getconn();
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		StringBuilder sb =new StringBuilder();
		sb.append("  INSERT INTO movie_director(movie_id,director_id)   ");
		sb.append("  SELECT movie.id,director.id FROM movie,director WHERE movie.id=?  ");
		sb.append("  and director.id IN ("+ids+")");
		
		String sql = sb.toString();
		
		try {
			ps=co.prepareStatement(sql);
			ps.setInt(1, movieId);
			
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			a.close(rs, ps, co);
		}
				
	}
}
